package com.wechat.service.storage;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.swak.annotation.FluxService;
import com.swak.entity.YesNo;
import com.swak.service.BaseService;
import com.swak.utils.Lists;
import com.swak.utils.Maps;
import com.swak.utils.Sets;
import com.swak.utils.StringUtils;
import com.wechat.Cons;
import com.wechat.dto.ChatRoomDTO;
import com.wechat.dto.ContactDTO;
import com.wechat.entity.ChatRoom;
import com.wechat.enums.ContactType;
import com.wechat.login.IDGens;
import com.wechat.login.Login;
import com.wechat.login.LoginService;
import com.wechat.service.ChatRoomService;
import com.wechat.service.ContactService;
import com.wechat.service.storage.mapper.ChatRoomMapper;

/**
 * 描述
 * 
 * @author Auto Generator
 * @date 2022-10-21T23:59:27.041546800
 */
@FluxService
@Service("wechatChatRoomService")
public class ChatRoomServiceImpl extends BaseService<ChatRoom, String> implements ChatRoomService {

	@Autowired
	private ChatRoomMapper chatRoomMapper;
	@Autowired
	private ContactService contactService;

	@Override
	public ChatRoomDTO get(String id) {
		return ChatRoomDTO.convert(this.chatRoomMapper.get(id));
	}

	@Override
	@Transactional
	public ChatRoomDTO update(ChatRoomDTO room) {
		ChatRoom.of().setId(room.getId()).setDisplayName(room.getDisplayName()).setFlag(room.getFlag())
				.setAnnouncement(room.getAnnouncement()).setAnnouncementEditor(room.getAnnouncementEditor())
				.setAnnouncementPublish(room.getAnnouncementPublish()).setManageUser(room.getManageUser())
				.updateSelective();
		return this.get(room.getId());
	}

	@Override
	@Transactional
	public ChatRoomDTO create(List<ContactDTO> contacts) {

		// 重新查询联系人的信息，并去除重复的数据
		Map<String, ContactDTO> addContacts = Maps.newHashMap();
		for (ContactDTO contact : contacts) {
			addContacts.put(contact.getId(), this.contactService.get(contact.getId()));
		}

		// 重新指向新的数据集合
		contacts = Lists.newArrayList(addContacts.values());

		// 当前的登陆信息
		Login login = LoginService.login();

		// 基本的信息
		String userNames = StringUtils.join(contacts.stream().map(c -> c.getId()).toList(), Cons.UserNamesSeparateChar);
		String nickNames = StringUtils.join(contacts.stream().map(c -> c.getNickName()).toList(),
				Cons.UserNamesSeparateChar);
		String nickName = StringUtils.join(contacts.stream().limit(5).map(c -> c.getNickName()).toList(),
				Cons.NickNamesSeparateChar);
		// 最多100字符
		nickName = StringUtils.abbreviate(nickName, 100);

		// 创建 room
		ChatRoom room = ChatRoom.of().setId(IDGens.genRoomID()).setNickName(nickName).setUserNames(userNames)
				.setNickNames(nickNames).setCreateUser(login.getId()).setManageUser(login.getId())
				.setUserCount(addContacts.size()).insert();

		// 同时创建一个联系人
		this.contactService.create(ContactDTO.of().setId(room.getId()).setNickName(room.getNickName())
				.setType(ContactType.Chat_Room.getType()).setIsFriend(YesNo.No.getType()));

		// 返回数据
		return ChatRoomDTO.convert(room);
	}

	@Override
	@Transactional
	public ChatRoomDTO addContacts(ChatRoomDTO room, List<ContactDTO> contacts) {

		// 拉取最新的聊天室的数据
		ChatRoomDTO roomed = this.get(room.getId());

		// 联系人的列表
		Set<String> userNameSet = Sets.newOrderSet(roomed.getUserNames().split(Cons.UserNamesSeparateChar));

		// 重新查询联系人的信息，并去除重复的数据
		List<ContactDTO> adds = Lists.newArrayList();
		for (ContactDTO contact : contacts) {
			if (userNameSet.contains(contact.getId())) {
				continue;
			}
			adds.add(contact);
		}

		// 需要添加的联系人
		String userNames = StringUtils.join(contacts.stream().map(c -> c.getNickName()).toList(),
				Cons.UserNamesSeparateChar);
		String nickNames = StringUtils.join(contacts.stream().map(c -> c.getNickName()).toList(),
				Cons.UserNamesSeparateChar);

		// 重新设置聊天室的联系人列表
		ChatRoom.of().setUserNames(roomed.getUserNames() + Cons.UserNamesSeparateChar + userNames)
				.setNickNames(roomed.getNickNames() + Cons.UserNamesSeparateChar + nickNames).setId(roomed.getId())
				.updateSelective();

		// 返回数据
		return this.get(roomed.getId());
	}

	@Override
	@Transactional
	public Integer delete(ChatRoomDTO chatRoom) {
		ChatRoom entity = chatRoom.transform();
		return this.chatRoomMapper.delete(entity);
	}

	@Override
	@Transactional
	public ChatRoomDTO saveDisplayName(ChatRoomDTO chatRoom) {
		ChatRoom.of().setId(chatRoom.getId()).setDisplayName(chatRoom.getDisplayName()).updateSelective();
		return this.get(chatRoom.getId());
	}

	// ******************* 同步数据，不要级联更新其他数据 ******************
	@Override
	@Transactional
	public void syncNickName(ChatRoomDTO chatRoom) {
		ChatRoom.of().setId(chatRoom.getId()).setNickName(chatRoom.getNickName()).updateSelective();
	}
}
